<?php

use Biz\service\Album\AlbumListService;
use Biz\service\Album\SyncEsMainContentService;
use Doraemon\pockets\constInterface\EsDbFieldsMap;
use Doraemon\tools\IdGen;
use Doraemon\tools\IdParse;
use Doraemon\tools\Log;

class SyncMainContentAlbumCounterAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * @throws Exception
     */
    public function main()
    {
        Log::info('更新清单再maincontent索引的计数器数据', $this->messageBody);

        if (!$this->messageBody['table']) {
            throw new \Exception('未获取到数据表');
        }
        if (!$this->messageBody['data']) {
            throw new \Exception('未接收到数据');
        }
        if (in_array($this->messageBody['table'], EsDbFieldsMap::CONTENT_COUNTER_TABLE_MAP)) {

            $data = $this->messageBody['data'] ?? [];
            $objId = $data['obj_id'];

            if (!$objId) {
                return false;
            } else {
                $objType = IdParse::getType($objId);
                if (IdGen::TYPE_WIKI_ALBUM == $objType) {
                    $albumInfo = AlbumListService::getAlbumListCache([$objId])[$objId] ?? [];
                    if ($albumInfo) {
                        //更新清单在maincontent es索引里面的数据
                        $res = SyncEsMainContentService::syncAlbumToEsMainContent($objId, $albumInfo);
                        Log::info('更新清单在maincontent es索引里面的数据', ['$res' => $res]);
                    }
                }
            }
            Log::info('接收到事件消息:',['message'=>$this->messageBody,'$objId'=>$objId]);
        }
        $this->setSuccess();
    }
}